<?php
class Public_Post_CommentsSuccessView extends BlogPublicBaseView
{
    public function executeHtml(AgaviRequestDataHolder $rd)
    {
        $this->setupHtml($rd);
        $ro = $this->getContext()->getRouting();
        $params = $rd->getParameters();
        /* @var $comments_model Public_CommentsModel */
        $comments_model = $this->context->getModel('Comments', 'Public');
        $post = $this->getAttribute('post');
        //$comments_tree = array();
        $comments = self::processComments($comments_model->findBlogPostComments($post));
        
        /* @var $comments_model Public_PostsModel */
        $posts_model = $this->context->getModel('Posts', 'Public');
        $route_elements = $posts_model->getPostCoordinates($post);
        $post_url = $ro->gen('post.view', $route_elements, array('relative'=>FALSE));
        
        //var_dump($comments);
        $this->setAttributeByRef('comments', $comments);
        $this->setAttribute('params', $params);
        $this->setAttribute('post_url', $post_url);
    }
    
    protected static function processComments($xxx)
    {
        $res = array();
        
        foreach ($xxx as $e) {
            $comment = $e;
            $subcomments = $e->Children;
            
            $user_id = $comment->user_id;
            $user = $comment->User;
            $display_name = $comment->display_name;
            if (is_null($display_name)) {
                if ($comment->is_owner_comment) {
                    $display_name = WereWordConfig::get('blog.owner_name');
                } else {
                    $display_name = '%username%';
                }
            }
            
            $c = array(
                'id' => $comment->id,
                'time' => date_create($comment->comment_time),
                'text' => $comment->comment_text,
                'name' => $display_name,
                'email' => $comment->email,
                'website' => $comment->website,
                'is_user' => !is_null($user_id),
                'is_owner' => $comment->is_owner_comment,
                'approved' => $comment->is_approved,
                'type' => !is_null($user_id) ? $user->user_type : 'guest',
                'subcomments' => $subcomments
                );
            
            if (!is_null($user_id)) {
                $display_name = $user->display_name;
                if ($display_name != '') {
                    $c['name'] = $display_name;
                }
                
                $c['website'] = $user->website;
            }
            
            $c['subcomments'] = self::processComments($subcomments);
            $res[] = $c;
        }
        
        return $res;
    }
}
?>
